home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC-SIG: World of Games
/
PC-SIG World of Games (CDRM1080710) (1993).iso
/
ENT
/
DISK2468.ZIP
/
MM
/
MOUSE.C
< prev
next >
Wrap
Text File
|
1990-05-04
|
15KB
|
503 lines
/********************************************************************
* Mouse toolkit (mouse.c)
********************************************************************/
#include <bios.h>
#include <dos.h>
#include <conio.h>
#include <process.h>
#include <stdio.h>
#include "\tc\myincs\mouse.h"
/* Mouse cursor shapes are defined */
unsigned int standard_curs[16][2];
unsigned int up_arrow_curs[16][2];
/* standard cursor shape : screen mask array */
/* standard_curs[ 0][0] = 0x3fff; 0011111111111111 */
/* standard_curs[ 1][0] = 0x1fff; 0001111111111111 */
/* standard_curs[ 2][0] = 0x0fff; 0000111111111111 */
/* standard_curs[ 3][0] = 0x07ff; 0000011111111111 */
/* standard_curs[ 4][0] = 0x03ff; 0000001111111111 */
/* standard_curs[ 5][0] = 0x01ff; 0000000111111111 */
/* standard_curs[ 6][0] = 0x00ff; 0000000011111111 */
/* standard_curs[ 7][0] = 0x007f; 0000000001111111 */
/* standard_curs[ 8][0] = 0x003f; 0000000000111111 */
/* standard_curs[ 9][0] = 0x001f; 0000000000011111 */
/* standard_curs[10][0] = 0x01ff; 0000000000001111 */
/* standard_curs[11][0] = 0x10ff; 0001000011111111 */
/* standard_curs[12][0] = 0x30ff; 0011000011111111 */
/* standard_curs[13][0] = 0xf87f; 1111100001111111 */
/* standard_curs[14][0] = 0xf87f; 1111100001111111 */
/* standard_curs[15][0] = 0xfc3f; 1111110000111111 */
/* standard cursor shape : cursor mask array */
/* standard_curs[ 0][1] = 0x0000; 0000000000000000 */
/* standard_curs[ 1][1] = 0x4000; 0100000000000000 */
/* standard_curs[ 2][1] = 0x6000; 0110000000000000 */
/* standard_curs[ 3][1] = 0x7000; 0111000000000000 */
/* standard_curs[ 4][1] = 0x7800; 0111100000000000 */
/* standard_curs[ 5][1] = 0x7c00; 0111110000000000 */
/* standard_curs[ 6][1] = 0x7e00; 0111111000000000 */
/* standard_curs[ 7][1] = 0x7f00; 0111111100000000 */
/* standard_curs[ 8][1] = 0x7f80; 0111111110000000 */
/* standard_curs[ 9][1] = 0x78c0; 0111111111000000 */
/* standard_curs[10][1] = 0x7c00; 0111110000000000 */
/* standard_curs[11][1] = 0x4600; 0000011000000000 */
/* standard_curs[12][1] = 0x0600; 0000011000000000 */
/* standard_curs[13][1] = 0x0300; 0000001100000000 */
/* standard_curs[14][1] = 0x0300; 0000001100000000 */
/* standard_curs[15][1] = 0x0180; 0000000110000000 */
/* up arrow cursor shape : screen mask array */
/* up_arrow_curs[ 0][0] = 0xf9ff; 1111100111111111 */
/* up_arrow_curs[ 1][0] = 0xf0ff; 1111000011111111 */
/* up_arrow_curs[ 2][0] = 0xe07f; 1110000001111111 */
/* up_arrow_curs[ 3][0] = 0xe07f; 1110000001111111 */
/* up_arrow_curs[ 4][0] = 0xc03f; 1100000000111111 */
/* up_arrow_curs[ 5][0] = 0xc03f; 1100000000111111 */
/* up_arrow_curs[ 6][0] = 0x801f; 1000000000011111 */
/* up_arrow_curs[ 7][0] = 0x801f; 1000000000011111 */
/* up_arrow_curs[ 8][0] = 0x000f; 0000000000001111 */
/* up_arrow_curs[ 9][0] = 0x000f; 0000000000001111 */
/* up_arrow_curs[10][0] = 0xf0ff; 1111000011111111 */
/* up_arrow_curs[11][0] = 0xf0ff; 1111000011111111 */
/* up_arrow_curs[12][0] = 0xf0ff; 1111000011111111 */
/* up_arrow_curs[13][0] = 0xf0ff; 1111000011111111 */
/* up_arrow_curs[14][0] = 0xf0ff; 1111000011111111 */
/* up_arrow_curs[15][0] = 0xf0ff; 1111000011111111 */
/* up arrow cursor shape : cursor mask array */
/* up_arrow_curs[ 0][1] = 0x0000; 0000000000000000 */
/* up_arrow_curs[ 1][1] = 0x0600; 0000011000000000 */
/* up_arrow_curs[ 2][1] = 0x0f00; 0000111100000000 */
/* up_arrow_curs[ 3][1] = 0x0f00; 0000111100000000 */
/* up_arrow_curs[ 4][1] = 0x1f80; 0001111110000000 */
/* up_arrow_curs[ 5][1] = 0x1f80; 0001111110000000 */
/* up_arrow_curs[ 6][1] = 0x3fc0; 0011111111000000 */
/* up_arrow_curs[ 7][1] = 0x3fc0; 0011111111000000 */
/* up_arrow_curs[ 8][1] = 0x7fe0; 0111111111100000 */
/* up_arrow_curs[ 9][1] = 0x0600; 0000011000000000 */
/* up_arrow_curs[10][1] = 0x0600; 0000011000000000 */
/* up_arrow_curs[11][1] = 0x0600; 0000011000000000 */
/* up_arrow_curs[12][1] = 0x0600; 0000011000000000 */
/* up_arrow_curs[13][1] = 0x0600; 0000011000000000 */
/* up_arrow_curs[14][1] = 0x0600; 0000011000000000 */
/* up_arrow_curs[15][1] = 0x0000; 0000000000000000 */
/* mouse cursor : horizontal and vertical hot spots are defined */
#define standard_horiz_hot_spot -1
#define standard_vert_hot_spot -1
#define up_arrow_horiz_hot_spot -1
#define up_arrow_vert_hot_spot -1
/* <<<< Global variables >>>> */
int mouse_text_x; /* X posn of mouse in Turbo C text coordinates */
int mouse_text_y; /* Y posn of mouse in Turbo C text coordinates */
int mouse_grph_x; /* X posn of mouse in graphics coordinates */
int mouse_grph_y; /* Y posn of mouse in graphics coordinates */
int mouse_initialized = 0; /* Set to 1 if mouse_init() succeeds */
/* <<<< Internal variables >>>> */
/* Previous cursor state. */
/* 0 = mouse previously off, 1 means prev on. */
static int prev_cursor_state = 0;
/* Pointer to start of bios video data */
static char far *bios_video_area = (char far *)0x00400049L;
/* This variable i for graphics mode only. */
/* low_resolution = 1 if using 320x200 graphics */
static int low_resolution = 0; /* Leave at 0 for text mode */
static void set_mouse_posn(int *x, int *y); /* internal function */
static int low_res_mode(int gd, int gm); /* internal function */
void mouse(int *m1, int *m2, int *m3, int *m4)
/*
C to mouse driver interface via interrupt 0x33.
Only supports functions 0-10.
See your mouse technical reference manual for the
interface to other functions
*/
{
union REGS inregs, outregs;
inregs.x.ax = *m1;
inregs.x.bx = *m2;
inregs.x.cx = *m3;
inregs.x.dx = *m4;
int86(0x33,&inregs,&outregs);
*m1 = outregs.x.ax;
*m2 = outregs.x.bx;
*m3 = outregs.x.cx;
*m4 = outregs.x.dx;
}
int check_mouse_driver(int need_mouse)
/*
* If need_mouse = 1, then abort program if no mouse.
* Otherwise, if no mouse return 0, else return 1.
*/
{
void far *address;
/* get mouse interrupt vector address */
address = getvect(0x33);
/* look for NULL address or IRET instruction */
if ((address == NULL) || (*(unsigned char *)address == 0xcf))
{
if (need_mouse)
{
printf("Mouse driver NOT installed\n");
exit(1);
}
else return 0;
}
return 1;
}
int init_mouse(int need_mouse, int gd, int gm)
/*
Initializes the mouse. If it can't, and need_mouse = 1,
this routine exits the program. Else, it returns
mouse_initialized = 1.
If using the mouse for graphics, there are two special cases:
(1) If using Hercules graphics, we must let the mouse
driver know we're really in graphics mode, cause it
can't tell.
(2) If using any graphics mode with 320 pixels horizontally,
(ie. low resolution), we must make sure that the mouse
coordinates are scaled properly.
These cases are detected by the parameters gd (graphics driver),
and parameters gm (graphics mode), as defined by Turbo C graphics.
If you're just using text mode, set both to zero.
When fixing up Hercules graphics, we're assuming page 0, If using
page 1, set *bios_video_area = 5.
*/
{
int m1;
mouse_initialized = 0;
if (check_mouse_driver(need_mouse))
{
if (gd == 7) *bios_video_area = 6; /* Fix up Hercules mode */
if (low_res_mode(gd,gm))
low_resolution = 1; /* Fix up low resolution mode */
m1 = mouse_reset(); /* Start mouse at ground zero */
if (m1)
{
mouse_initialized = 1; /* Set mouse init flag */
move_mouse(0,0); /* Set coords to top of screen */
mouse_on(0); /* Turn mouse on for first time */
}
else
{
if (need_mouse)
{
printf("ERROR activating mouse ... |n");
exit(1);
}
}
}
return mouse_initialized;
}
static int low_res_mode(int gd, int gm)
/* Returns 1 if in any graphics mode with 320 pixels horizontally
Returns 0 otherwise
*/
{
if (
(gd == 1 || gd == 2 || gd == 8) && /* CGA, MCGA, ATT400 */
(gm >= 0 && gm <= 3) /* 320 hz mode */
)
return 1;
return 0;
}
int mouse_reset(void)
/* If the mouse was on, it turns it off.
Then, the mouse state is reset.
*/
{
int x1, m1, m2, m3, m4;
mouse_off(1); /* Turn off only if it was on */
m1 = M_RESET;
mouse(&m1, &m2, &m3, &m4);
set_mouse_posn(&m3, &m4); /* Initialize coordinates */
return m1;
}
void move_mouse(int x, int y)
/* Move mouse cursor to text position (x,y) */
{
int m1, m2, m3, m4;
if (!mouse_initialized) return;
m1 = M_SET_CURS;
m3 = x*8; m4 = y*8; /* convert to pixel coordinates */
mouse(&m1, &m2, &m3, &m4);
set_mouse_posn(&m3, &m4);
}
void mouse_on(int restoreflag)
/* restoreflag = 0 means you want the mouse on regardless of previous state.
restoreflag = 1 means you want the mouse on only if it was on previously.
*/
{
int m1, m2, m3, m4;
if (mouse_initialized)
{
if (!restoreflag || prev_cursor_state)
{
m1 = M_SHOW_CURS;
mouse(&m1, &m2, &m3, &m4);
prev_cursor_state = 1;
}
}
}
void mouse_off(int tempflag)
/* If tempflag = 1, it means you want to turn the mouse off, and if
it had been on set previous state to on.
If tempflag = 0, it means you want to turn mouse off, and regardless
off whether it was on or not, set previous state to off.
*/
{
int m1, m2, m3, m4;
if (mouse_initialized)
{
if (prev_cursor_state) /* Turn it off only if it was on */
{
m1 = M_HIDE_CURS;
mouse(&m1, &m2, &m3, &m4);
/* leave prev_cursor_state alone if just turning off temporarily */
if (!tempflag) prev_cursor_state = 0;
}
}
}
void mouse_grph_posn(int *x, int *y)
/* Returns the mouse graphics coordinates if the mouse is
initialized, else it returns (0,0).
*/
{
int m1, m2;
if (mouse_initialized)
{
m1 = M_GET_STATUS;
mouse(&m1, &m2, x, y);
set_mouse_posn(x, y);
}
else {
*x = 0; *y = 0; /* default to left corner */
}
return;
}
void mouse_txt_posn(int *x, int *y)
/* Returns the mouse text coordinates if the mouse is
initialized, else it returns (1,1).
*/
{
mouse_grph_posn(x,y);
*x = mouse_text_x;
*y = mouse_text_y;
return;
}
int mouse_in_box(int graphflag, int left, int top, int right, int bottom)
/* Returns 1 if the mouse is in the box given by the
coordinates, 0 otherwise. The type of coordinates is
given by graphflag. If graphflag = 1, the coordinates
are assumed to be graphics coordinates, else they're
text coordinates. Either way they are absolute coordinates.
In order for this routine to work porperly you must first
call mouse_txt_posn, mouse_grph_posn, or any of the
mouse funcitons that set the global mouse position variables.
If mouse not initialized, it returns 0;
*/
{
int x, y;
if (mouse_initialized)
{
if (graphflag)
{
x = mouse_grph_x;
y = mouse_grph_y;
}
else
{
x = mouse_text_x;
y = mouse_text_y;
}
if ((y >= top) && (y <= bottom) &&
(x >= left) && (x <= right)) return 1;
}
return 0;
}
int button_release(int b)
/* Looks for a left (b=0) or right (b=1) mouse button release.
Returns 1 if the button has been released since the last time
called, else returns 0. If no mouse installed, it returns 0.
*/
{
int m1, m2, m3, m4;
if (mouse_initialized)
{
m1 = M_GET_REL;
m2 = b; /* which button */
mouse(&m1, &m2, &m3, &m4);
set_mouse_posn(&m3, &m4);
if (m2) return 1;
}
return 0;
}
int button_press(int b)
/* Looks for a left (b=0) or right (b=1) mouse button press.
Returns 1 if the button has been released since the last time
called, else returns 0. If no mouse installed, it returns 0.
*/
{
int m1, m2, m3, m4;
if (mouse_initialized)
{
m1 = M_GET_PRESS;
m2 = b; /* which button */
mouse(&m1, &m2, &m3, &m4);
set_mouse_posn(&m3, &m4);
if (m2) return 1;
}
return 0;
}
int button_state()
/* Returns up/down state of the mouse buttons. A button is
down if its corresponding bit = 1.
Bit 2 Middle button (Logitech mouse only)
1 Right button
LSB 0 Left button
Returns 0x00 if no button down or no mouse installed.
Also stores mouse position information.
*/
{
int m1, m2, m3, m4;
if (mouse_initialized)
{
m1 = M_GET_STATUS;
mouse(&m1, &m2, &m3, &m4);
set_mouse_posn(&m3, &m4);
return m2;
}
return 0;
}
static void set_mouse_posn(int *x, int *y)
/* Sets the internal mouse position variables
Corrects for 320x200 low resolution modes
*/
{
if (low_resolution) *x >>= 1; /* divide by two */
mouse_grph_x = *x;
mouse_grph_y = *y;
mouse_text_x = *x/8 + 1;
mouse_text_y = *y/8 + 1;
}
int mouse_trigger(int button_dir)
/* Looks for a key press, or a button release (if button_dir = 0)
or press (if button_dir = 1). Key presses have priority over
mouse buttons. The left mouse button has priority over the
right button. If a key has been pressed, it is then removed
from the keyboard buffer.
*/
{
int k;
if (bioskey(1))
{
k = bioskey(0);
}
else
{
k = 0;
if (button_dir)
{
if (button_press(0)) k = LEFT_MOUSE_PRESS;
else if (button_press(1)) k = RIGHT_MOUSE_PRESS;
}
else
{
if (button_release(0)) k = LEFT_MOUSE_REL;
else if (button_release(1)) k = RIGHT_MOUSE_REL;
}
}
return k;
}
void mouse_debounce(void)
/* Debounces mouse buttons by waiting 10 milliseconds. */
{
int x1, m1, m2, m3, m4;
delay(10);
m1 = M_CHANGE_CURS;
do
{
mouse(&m1, &m2, &m3, &m4);
}
while (m2 != 0);
}
int mouse_cursor(int *cursor, int horiz, int vert)
/* Mouse cursor shapes are set. */
{
int m1, m2, m3, m4;
if (mouse_initialized)
{
m1 = 9;
m2 = horiz; /* horizontal hot spot */
m3 = vert; /* vertical hot spot */
mouse(&m1, &m2, &m3, cursor);
return 1;
}
return 0;
}
int set_mouse_xbounds(int left, int right)
/* set x-axis limits for mouse */
{
int m1, m2, m3, m4;
if (mouse_initialized)
{
m1 = M_SET_X_BOUNDS;
m3 = left*8 - 1; m4 = right*8 - 1; /* convert to pixel coordinates */
mouse(&m1, &m2, &m3, &m4);
set_mouse_posn(&m3, &m4);
}
}
int set_mouse_ybounds(int top, int bottom)
/* set y-axis limits for mouse */
{
int m1, m2, m3, m4;
if (mouse_initialized)
{
m1 = M_SET_Y_BOUNDS;
m3 = top*8 - 1; m4 = bottom*8 - 1; /* convert to pixel coordinates */
mouse(&m1, &m2, &m3, &m4);
set_mouse_posn(&m3, &m4);
}
}
int set_mouse_window(int left, int top, int right, int bottom)
/* set x-axis and y-axis window for mouse. If left and/or
are CTRWIN it means that window should be centered on the screen
and that right and/or bottom are entire window size */
{
if (mouse_initialized)
{
if (left == CTRWIN) { /* means that right = width of entire window */
left = ((80-right)/2); /* corrected left value */
right = left + right - 1; /* corrected right value */
}
if (top == CTRWIN) { /* means that bottom = height of entire window */
top = ((25-bottom)/2);
bottom = top + bottom - 1;
}
set_mouse_xbounds(left,right);
set_mouse_ybounds(top,bottom);
}
}